package com.scheduler.backend.controller;

import com.scheduler.backend.bl.OrderService;
import com.scheduler.backend.vo.ResponseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController()
@CrossOrigin
@Tag(name = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/getOrderGanttByDate/{date}", method = RequestMethod.GET)
    @Operation(summary = "根据Date获取订单甘特图", description = "根据Date获取订单甘特图", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getOrderGanttByDate(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                          HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.getOrderGanttByDate(date);
    }

    @RequestMapping(value = "/order/getNewOrders/{date}", method = RequestMethod.GET)
    @Operation(summary = "获取未排期的订单", description = "获取未排期的订单", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getNewOrders(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                          HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.getNewOrders(date);
    }

    @RequestMapping(value = "/order/getAllOrders/{date}", method = RequestMethod.GET)
    @Operation(summary = "获取已排期的订单", description = "获取已排期的订单", method = "GET")
    @Parameter(name = "date", description = "日期", required = true)
    public ResponseVO getAllOrders(@PathVariable("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                   HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.getAllOrders(date);
    }

    @RequestMapping(value = "/product/getAllProducts", method = RequestMethod.GET)
    @Operation(summary = "获取所有成品", description = "获取所有成品", method = "GET")
    public ResponseVO getAllProducts(HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.getAllProducts();
    }

    @RequestMapping(value = "/order/getOrderGantt", method = RequestMethod.GET)
    @Operation(summary = "获取订单甘特图", description = "获取订单甘特图", method = "GET")
//    @Parameter(name = "date", description = "日期", required = true)
//    @Parameter(name = "orderId", description = "订单id", required = true)
    public ResponseVO getOrderGantt(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                    @RequestParam  String orderId,
                                    HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.getOrderGantt(date,orderId);
    }


    @RequestMapping(value = "/order/deleteOrder/{orderId}", method = RequestMethod.POST)
    @Operation(summary = "删除订单", description = "删除订单", method = "POST")
    @Parameter(name = "orderId", description = "订单Id", required = true)
    public ResponseVO deleteOrder(@PathVariable("orderId") String orderId,
                                          HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.deleteOrder(orderId);
    }


    @RequestMapping(value = "/order/addOrder/{orderId}/{productId}/{productNum}/{deadline}", method = RequestMethod.POST)
    @Operation(summary = "新增/修改订单", description = "新增/修改订单", method = "POST")
    @Parameter(name = "orderId", description = "订单Id", required = true)
    @Parameter(name="productId",description="产品Id",required=true)
    @Parameter(name="productNum",description="产品数量",required=true)
    @Parameter(name="deadline",description="订单截止日期",required=true)
    public ResponseVO addOrder(@PathVariable("orderId") String orderId,
                                  @PathVariable("productId") String productId,
                                  @PathVariable("productNum") int productNum,
                                  @PathVariable("deadline") @DateTimeFormat(pattern="yyyy-MM-dd") Date deadline,
                                  HttpServletResponse response) {
        if (response != null) {
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Cache-Control", "no-cache");
        }
        return orderService.addOrder(orderId,productId,productNum,deadline);
    }



}
